package org.jeecg.modules.purchase.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.purchase.entity.PurchaseStock;
import org.jeecg.modules.purchase.entity.PurchaseStockOut;
import org.jeecg.modules.purchase.entity.PurchaseStockOutGoods;
import org.jeecg.modules.purchase.mapper.PurchaseStockOutGoodsMapper;
import org.jeecg.modules.purchase.mapper.PurchaseStockOutMapper;
import org.jeecg.modules.purchase.service.IPurchaseStockOutService;
import org.jeecg.modules.purchase.service.IPurchaseStockService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
* @Description: 出库记录
* @Author: jeecg-boot
* @Date:   2022-11-15
* @Version: V1.0
*/
@Service
public class PurchaseStockOutServiceImpl extends ServiceImpl<PurchaseStockOutMapper, PurchaseStockOut> implements IPurchaseStockOutService {
	@Autowired
	private PurchaseStockOutMapper purchaseStockOutMapper;
	@Autowired
	private PurchaseStockOutGoodsMapper purchaseStockOutGoodsMapper;
	@Autowired
	private ISysBaseAPI sysBaseAPI;

	@Resource
	private IPurchaseStockService purchaseStockService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PurchaseStockOut purchaseStockOut, List<PurchaseStockOutGoods> purchaseStockOutGoodsList) {
		purchaseStockOutMapper.insert(purchaseStockOut);
		if(purchaseStockOutGoodsList!=null && purchaseStockOutGoodsList.size()>0) {
			for(PurchaseStockOutGoods entity:purchaseStockOutGoodsList) {
				//外键设置
				entity.setStockOutId(purchaseStockOut.getId());
				entity.setStockOutCode(purchaseStockOut.getCode());
				// 生成库存类
				PurchaseStock purchaseStock = createPurchaseStock(entity,entity.getCode(),entity.getName());
				purchaseStock.setTotalNumber(entity.getNumber().negate());
				// 出库需要对应减少库存中物品数量
				purchaseStockService.addStock(purchaseStock);
				purchaseStockOutGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMainTemp(PurchaseStockOut purchaseStockOut, List<PurchaseStockOutGoods> purchaseStockOutGoodsList) {
		if(StringUtils.isBlank(purchaseStockOut.getCode())){
			String code = null;
			Result result = sysBaseAPI.executeByRuleCode("StockInCodeRule",new JSONObject());
			if(result.isSuccess()){
				code = (String)result.getResult();
			}
			purchaseStockOut.setCode(code);
		}
		purchaseStockOutMapper.insert(purchaseStockOut);
		if(purchaseStockOutGoodsList!=null && purchaseStockOutGoodsList.size()>0) {
			for(PurchaseStockOutGoods entity:purchaseStockOutGoodsList) {
				//外键设置
				entity.setStockOutId(purchaseStockOut.getId());
				purchaseStockOutGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PurchaseStockOut purchaseStockOut,List<PurchaseStockOutGoods> purchaseStockOutGoodsList) {

		// 1.获取原出库单
		// 获取原出库单
		PurchaseStockOut oriPurchaseStockOut = this.getById(purchaseStockOut.getId());
		// 更新出库单
		this.updateById(purchaseStockOut);
		// 获取出库物品
		LambdaQueryWrapper<PurchaseStockOutGoods> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PurchaseStockOutGoods::getStockOutId,oriPurchaseStockOut.getId());
		List<PurchaseStockOutGoods> oriPurchaseStockOutGoodsList = purchaseStockOutGoodsMapper.selectList(wrapper);

		// 2.撤销原出库单下的物品记录
		this.delPurchaseStockOutGoods(oriPurchaseStockOut,oriPurchaseStockOutGoodsList);

		// 3.添加新编辑的物品记录
		this.savePurchaseStockOutGoods(purchaseStockOut,purchaseStockOutGoodsList);

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		PurchaseStockOut purchaseStockOut = this.getById(id);
		// 撤销已经出库的库存
		LambdaQueryWrapper<PurchaseStockOutGoods> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(PurchaseStockOutGoods::getStockOutId,id);
		// 获取出库单中对应的物品
		List<PurchaseStockOutGoods> oriPurchaseStockOutGoodsList = purchaseStockOutGoodsMapper.selectList(wrapper);
		// 遍历修改库存
		oriPurchaseStockOutGoodsList.forEach(entity->{
			PurchaseStock purchaseStock = createPurchaseStock(entity, entity.getCode(), entity.getName());
			purchaseStock.setTotalNumber(entity.getNumber());
			purchaseStockService.addStock(purchaseStock);
		});
		purchaseStockOutGoodsMapper.deleteByMainId(id);
		purchaseStockOutMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			PurchaseStockOut purchaseStockOut = this.getById(id);
			// 撤销已经出库的库存
			LambdaQueryWrapper<PurchaseStockOutGoods> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(PurchaseStockOutGoods::getStockOutId,id);
			// 获取出库单中对应的物品
			List<PurchaseStockOutGoods> oriPurchaseStockOutGoodsList = purchaseStockOutGoodsMapper.selectList(wrapper);
			// 遍历修改库存
			oriPurchaseStockOutGoodsList.forEach(entity->{
				PurchaseStock purchaseStock = createPurchaseStock(entity, entity.getCode(), entity.getName());
				purchaseStock.setTotalNumber(entity.getNumber());
				purchaseStockService.addStock(purchaseStock);
			});
			purchaseStockOutGoodsMapper.deleteByMainId(id.toString());
			purchaseStockOutMapper.deleteById(id);
		}

	}

	@Override
	public IPage<PurchaseStockOut> pageList(Page<PurchaseStockOut> page, QueryWrapper<PurchaseStockOut> queryWrapper) {
		return purchaseStockOutMapper.getPageList(page,queryWrapper);
	}

	// 撤销出库物品
	private void delPurchaseStockOutGoods(PurchaseStockOut purchaseStockOut,List<PurchaseStockOutGoods> oriPurchaseStockOutGoodsList) {
		// 遍历修改库存
		oriPurchaseStockOutGoodsList.forEach(entity->{
			PurchaseStock purchaseStock = createPurchaseStock(entity, entity.getCode(), entity.getName());
			purchaseStock.setTotalNumber(entity.getNumber());
			purchaseStockService.addStock(purchaseStock);
		});
		purchaseStockOutGoodsMapper.deleteByMainId(purchaseStockOut.getId());
	}

	// 添加出库物品
	public void savePurchaseStockOutGoods(PurchaseStockOut purchaseStockOut, List<PurchaseStockOutGoods> purchaseStockOutGoodsList) {
		if(purchaseStockOutGoodsList!=null && purchaseStockOutGoodsList.size()>0) {
			for(PurchaseStockOutGoods entity:purchaseStockOutGoodsList) {
				//外键设置
				entity.setStockOutId(purchaseStockOut.getId());
				entity.setStockOutCode(purchaseStockOut.getCode());
				// 生成库存类
				PurchaseStock purchaseStock = createPurchaseStock(entity,entity.getCode(),entity.getName());
				purchaseStock.setTotalNumber(entity.getNumber().negate());
				// 出库需要对应减少库存中物品数量
				purchaseStockService.addStock(purchaseStock);
				purchaseStockOutGoodsMapper.insert(entity);
			}
		}
	}


	private PurchaseStock createPurchaseStock(PurchaseStockOutGoods entity,String repositoryCode, String repositoryName){
		PurchaseStock purchaseStock = new PurchaseStock();
		purchaseStock.setRepositoryName(repositoryName);
		purchaseStock.setRepositoryCode(repositoryCode);
		purchaseStock.setGoodsName(entity.getGoodsName());
		purchaseStock.setGoodsTypeCode(entity.getGoodsTypeCode());
		purchaseStock.setGoodsCode(entity.getGoodsCode());
		purchaseStock.setBrand(entity.getBrand());
		purchaseStock.setGoodsSize(entity.getSize());
		purchaseStock.setGoodsUnit(entity.getUnit());
		purchaseStock.setGoodsUnitPrice(entity.getUnitPrice());
		return purchaseStock;
	}
}
